﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//
namespace Serotonin.OptionsForms
{
    using Controls;
    using Filter;
    using Helper;
    using System;
    using System.Drawing;
    using System.Globalization;
    using System.Windows.Forms;

    /// <summary>Filter specified properties form.</summary>
    public partial class ConGrapForm : Form, IOptionsForm
    {
        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }

        private readonly ConGrap conGrap;        
        private bool doDilatation;
        /// <summary>
        /// Initializes a new instance of the <see cref="ConGrapForm"/> class.
        /// </summary>
        /// <param name="processor">The processor.</param>
        public ConGrapForm(Processor processor)
        {
            InitializeComponent();
            conGrap = AOF.Init(processor, this, gbProcess,
                                new ConGrap()) as ConGrap;

            #region Pre-Configure
            //trbarWeight.Value = 20;
            //trbarMinContourSize.Value = 300;
            //trbarTraceRadius.Value = 10;
            //chboxDrawBorderLine.Checked = true;

            //trbarHighThreshold.Value = 20;
            //trbarLowThreshold.Value = 10;
            //trbarGaussSize.Value = 5;
            //trbarGaussSigma.Value = (int)(1.631 * 1000 + 0.1);
            #endregion Pre-Configure
        }

        /// <summary>
        /// Applies the specified filter in the options form.
        /// </summary>
        public void ApplyFilter()
        {
            Bitmap b;

            // Todo FF: Hack for rectangle border and dilatation
            if (doDilatation)
            {
                int tmp = conGrap.LineRectStrength;
                conGrap.LineRectStrength = 1;
                b = conGrap.Apply(Processor.OriginalImage);
                conGrap.LineRectStrength = tmp;

                SimpleDilatation dilatation = new SimpleDilatation();
                for (int i = 0; i < conGrap.LineRectStrength; i++)
                {
                    b = dilatation.Apply(b);
                }
            }
            else
            {
                b = conGrap.Apply(Processor.OriginalImage);
            }

            string sDoDilatation = chboxDoDilatations.Checked
                                              ? Program.L.Text[35]
                                              : Program.L.Text[34];
            string sBorderRect = chboxDrawBorderRect.Checked
                                              ? Program.L.Text[35]
                                              : Program.L.Text[34];
            string sBorderLine = chboxDrawBorderLine.Checked
                                              ? Program.L.Text[35]
                                              : Program.L.Text[34];

            string s = Program.L.Text[40] + Program.L.Text[9] + ": " +
                Program.L.Text[51] + " " + lbContourNumbers.Text + "; " +
                Program.L.Text[43] + "=" + lbWeight.Text + "; " +
                Program.L.Text[44] + "=" + lbTraceRadius.Text + "; " +
                Program.L.Text[46] + "=" + sDoDilatation + "; " +
                Program.L.Text[47] + "=" + sBorderRect + "; " +
                Program.L.Text[48] + "=" + sBorderLine + "; " +
                Program.L.Text[49] + "=" + lbLineStrength.Text +
                "; High=" + conGrap.HighThreshold + 
                "; Low=" + conGrap.LowThreshold +
                "; Size=" + conGrap.GaussianSize + 
                "; Sigma=" + conGrap.GaussianSigma;
           
            ColorPalette.SetColorPalette(b);
            Processor.Change(b, s); 
            lbContourNumbers.Text = conGrap.Contours.Count.ToString(
                CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[42];
            lbWeightText.Text = Program.L.Text[43];
            lbTraceRadiusText.Text = Program.L.Text[44];
            gbDrawModes.Text = Program.L.Text[45];
            chboxDoDilatations.Text = Program.L.Text[46];
            chboxDrawBorderRect.Text = Program.L.Text[47];
            chboxDrawBorderLine.Text = Program.L.Text[48];
            lbLineStrengthName.Text = Program.L.Text[49];
            gbResults.Text = Program.L.Text[50];
            lbContourNumbersText.Text = Program.L.Text[51];

            tabctrlOptions.TabPages[0].Text = Program.L.Text[52];
            tabctrlOptions.TabPages[1].Text = Program.L.Text[53];
        }

        #region Canny tab control events

        private void TrbarHighThresholdValueChanged(object sender, EventArgs e)
        {
            conGrap.HighThreshold = (byte)trbarHighThreshold.Value;
            lbHighThreshold.Text = trbarHighThreshold.Value.ToString(
                CultureInfo.InvariantCulture);
        }

        private void TrbarLowThresholdValueChanged(object sender, EventArgs e)
        {
            conGrap.LowThreshold = (byte)trbarLowThreshold.Value;
            lbLowThreshold.Text = trbarLowThreshold.Value.ToString(
                CultureInfo.InvariantCulture);
        }

        private void TrbarGaussSizeValueChanged(object sender, EventArgs e)
        {
            conGrap.GaussianSize = trbarGaussSize.Value;
            lbGaussSize.Text = trbarGaussSize.Value.ToString(
                CultureInfo.InvariantCulture);
        }

        private void TrbarGaussSigmaValueChanged(object sender, EventArgs e)
        {
            conGrap.GaussianSigma = trbarGaussSigma.Value / 1000.0f;
            lbGaussSigma.Text = (trbarGaussSigma.Value / 1000.0f).ToString(
                CultureInfo.InvariantCulture);
        }
        #endregion Canny tab control events

        #region Contour tab control events

        private void ChboxDrawBorderRectCheckedChanged(object sender, EventArgs e)
        {
            conGrap.DrawBorderRectangle = ((CheckBox)sender).Checked;
        }

        private void ChboxDrawBorderLineCheckedChanged(object sender, EventArgs e)
        {
            conGrap.OnlyBorderLine = ((CheckBox)sender).Checked;
        }

        private void ChboxDoDilatationsCheckedChanged(object sender, EventArgs e)
        {
            doDilatation = ((CheckBox)sender).Checked;
        }

        private void TrbarRectStrengthValueChanged(object sender, EventArgs e)
        {
            conGrap.LineRectStrength = (byte)trbarStrength.Value;
            lbLineStrength.Text = trbarStrength.Value.ToString(
                CultureInfo.InvariantCulture);
        }

        private void TrbarTraceRadiusValueChanged(object sender, EventArgs e)
        {
            conGrap.TraceRadius = (byte)trbarTraceRadius.Value;
            lbTraceRadius.Text = trbarTraceRadius.Value.ToString(
                CultureInfo.InvariantCulture);
        }

        private void TrbarWeightValueChanged(object sender, EventArgs e)
        {
            conGrap.Weight = (byte)trbarWeight.Value;
            lbWeight.Text = trbarWeight.Value.ToString(
                CultureInfo.InvariantCulture);
        }

        #endregion Contour tab control events

        private void BtnProcessClick(object sender, EventArgs e)
        {
            EndlessProgressBarFormInThread form =
                new EndlessProgressBarFormInThread(
                    Program.L.Text[63],
                    Program.L.Text[64]);
            form.Start();

            try
            {
                ApplyFilter();
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }

            form.End();
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            Processor.Reset();
            lbContourNumbers.Text = string.Format(@"0");
        }
    }
}
